package com.aegean.onlinebook.service.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.Hibernate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.onlinebook.model.ServiceValue;
import com.aegean.onlinebook.model.component.Photo;
import com.aegean.onlinebook.service.business.ServiceBusiness;
import com.opensymphony.xwork2.ActionSupport;

@Controller("AddServiceAction")
@Scope("prototype")
public class RegisterAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	ServiceBusiness business;
	private ServiceValue service;
	private File photo;
	private String photoFileName;
	private String photoContentType;
	private String msg;
    private boolean success;
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public ServiceValue getService() {
		return service;
	}

	public void setService(ServiceValue service) {
		this.service = service;
	}
    
	@JSON(serialize=false)
	@Override
	public String execute() throws Exception {
		System.out.println("1111");
		try {
			Photo p = new Photo();
			InputStream in = new FileInputStream(photo);
			p.setPhoto(Hibernate.createBlob(in));
			p.setPhoto_Name(photoFileName);
			p.setPhoto_Type(photoContentType);
			service.setUser_Photo(p);
			business.insertService(service);
			success=true;
			msg="恭喜您，注册成功！";
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			msg="对不起，注册失败！";
			success=false;
			return "fuck";
		}
	}
}
